package com.burgeon.backend.SHOP.rest;
/**
 * 积分商城创建零售单
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.Address;
import com.burgeon.backend.SHOP.entity.Mail;
import com.burgeon.backend.SHOP.entity.Store;
import com.burgeon.backend.entity.Coupon;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class CreateYCRETAIL extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateYCRETAIL(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }
    /**
     * 发送并接收字符
     */
    public String getData(String orderType, ArrayList<ArrayList> RETAIL,
                          String totalPrice, String phone, String orderid,
                          Mail mail, Address address, Coupon coupon , ArrayList<Store> stores,
                          String comment,String emp) throws Exception {
        System.err.println("emp="+emp);
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(orderType,RETAIL,totalPrice,phone,orderid,mail,address,coupon,stores,comment,emp), url);
        System.err.println(str);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)) {
            throw new Exception(message);
        }
        return new JSONArray(str).getJSONObject(0).getString("objectid");
    }

    private MultiValueMap Data(
            String orderType,ArrayList<ArrayList> RETAIL, String totalPrice,String phone,
            String orderid, Mail mail, Address address,Coupon coupon,ArrayList<Store> stores,
            String comment,String emp) throws Exception {

        HashMap params = new HashMap();
        HashMap masterobj = new HashMap(); //头表记录对象
        masterobj.put("table", "YC_RETAIL");
        masterobj.put("ID", "-1");
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(now);
        masterobj.put("BILLDATE", dateString);
        masterobj.put("XSNO", orderid);
        masterobj.put("YC_SOTYPE",orderType);
        masterobj.put("RECEIVER_NAME",address.getReceiver());
        masterobj.put("RECEIVER_MOBILE",address.getPhone());
        masterobj.put("RECEIVER_ADDRESS",address.getAddress());
        masterobj.put("CITY",address.getCity());
        masterobj.put("C_VIP_ID__CARDNO",phone);
        masterobj.put("IS_SELFRAIS","N");
        masterobj.put("DESCRIPTION","'"+comment+"'");
        HashMap detailobjs = new HashMap(); //明细表记录对象
        ArrayList list = new ArrayList();
        FIndAD_Table fIndAD_table = new FIndAD_Table(ip,account,password);
        HashMap yc_retail = fIndAD_table.getData("YC_RETAIL");
        list.add(yc_retail.get("YC_RETAILITEM"));//4435
        list.add(yc_retail.get("YC_FREIGHTITEM"));//4433
        list.add(yc_retail.get("YC_RETAILPAYITEM"));//4434
        list.add(yc_retail.get("YC_RETAILJUITEM"));//4454

        detailobjs.put("reftables", list);
//****************************************************************************
        ArrayList refobjs = new ArrayList();
        HashMap ref1 = new HashMap();
        ref1.put("table", "YC_RETAILITEM");//零售单明细

        List addList1 = new ArrayList();
        for (ArrayList data : RETAIL) {
            HashMap map1 = new HashMap();//addlist内容
            map1.put("id", "-1");
            map1.put("M_PRODUCT_ID__NAME", data.get(0));
            //map1.put("C_VIP_ID__CARDNO",phone);
            map1.put("QTY", data.get(1));
            map1.put("PRICEACTUAL", Double.parseDouble(data.get(2).toString()));
            map1.put("TOT_AMT_ACTUAL",Double.parseDouble(data.get(2).toString()));
            map1.put("TYPE", "1");
            map1.put("DESCRIPTION", "线上商城");
            map1.put("UNIQUEID",orderid);

            if (null!=emp&&emp.length()!=0&&!"".equals(emp)){
                map1.put("SALESREP_ID__NAME","'"+emp+"'");
            }

            addList1.add(map1);
        }

        ref1.put("addList", String.valueOf(addList1));

        refobjs.add(ref1);
        //************************************************
        HashMap ref2 = new HashMap();
        ref2.put("table", "YC_FREIGHTITEM");//运费明细
        ArrayList addList2 = new ArrayList();
        if (null!=coupon&&coupon.getMoney()!=null&&Integer.parseInt(coupon.getMoney())>0){
            HashMap map2 = new HashMap();
          //  map2.put("C_VIP_ID__CARDNO",phone);//客户id
            map2.put("FREIGHT_TYPE","6");
            map2.put("PAYAMOUNT",coupon.getMoney());
            map2.put("DESCRIPTION",coupon.getCode());
            addList2.add(map2);
        }
        HashMap map2 = new HashMap();
      //  map2.put("C_VIP_ID__CARDNO",phone);//客户id
        map2.put("FREIGHT_TYPE","1");
        map2.put("PAYAMOUNT",mail.getPrice());
        addList2.add(map2);

        ref2.put("addList", String.valueOf(addList2));

        refobjs.add(ref2);

        //************************************************

        HashMap ref3 = new HashMap();
        ref3.put("table", "YC_RETAILPAYITEM");//付款明细
        ArrayList addList3 = new ArrayList();
        HashMap map3 = new HashMap();
        map3.put("id", "-1");
        //map3.put("C_VIP_ID__CARDNO",phone);
        //map3.put("C_CURRENCY_ID__DESCRIPTION", "人民币");
        map3.put("C_PAYWAY_ID__NAME", "现金");
        //map3.put("TOT_AMT_ACTUAL", totalPrice);
        map3.put("PAYAMOUNT", Double.parseDouble(totalPrice));
        addList3.add(map3);

        ref3.put("addList", String.valueOf(addList3));

        refobjs.add(ref3);
        //**************************************************************
        HashMap ref4 = new HashMap();
        ref4.put("table", "YC_RETAILJUITEM");//零售单明细

        List addList4 = new ArrayList();
        for (Store store : stores) {
            HashMap map4 = new HashMap();//addlist内容
            map4.put("id", "-1");
            map4.put("C_STORE_ID__NAME", store.getName());
            map4.put("DISTANCE",store.getDistance());

            addList4.add(map4);
        }

        ref4.put("addList", String.valueOf(addList4));

        refobjs.add(ref4);
        //**************************************************

        detailobjs.put("refobjs", refobjs);

        params.put("masterobj", masterobj);
        params.put("detailobjs", detailobjs);
        //params.put("submit123", "true123");//是否提交 绝对不可
        System.err.println("*******************************");
        System.err.println(params);
        System.err.println("*******************************");
        return DATA("ProcessOrder", params, account, password);
    }

    public static void main(String[] args) {
        CreateYCRETAIL g = new CreateYCRETAIL("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        ArrayList<ArrayList> RETAIL = new ArrayList<ArrayList>();
        ArrayList list = new ArrayList();
        list.add("E1NBW22499E203"); //条码
        list.add("1");
        list.add("1");

        RETAIL.add(list);

        Mail mail = new Mail();
        Address address = new Address();
        Coupon coupon = new Coupon();
        coupon.setMoney("20");
        coupon.setCode("123");
        ArrayList<Store> stores = new ArrayList<>();
        try {
           System.err.println(g.getData("Z",RETAIL,"1","17732141036","132553",mail,address,coupon,stores,"","047"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
